package com.jh.cal;

public class Calendar 
{
	public final int[] LEAP = {31,29,31,30,31,30,31,31,30,31,30,31};

	public final int[] PLAIN = {31,28,31,30,31,30,31,31,30,31,30,31};

	public boolean isLeapYear(int year)
	{
		boolean isC = false;
		if ((year%4==0 && year%100!=0) || year%400==0) 
		{
			isC =true;
		}
		return isC;
	}

	public int days(int year)
	{
		int totalDay = 0;

		for (int i = 1; i < year; i++) 
		{
			if (isLeapYear(i)) 
			{
				totalDay += 366;
			}
			else
			{
				totalDay += 365;
			}
		}
		return totalDay;
	}

	public int days(int year, int month)
	{
		int totalDay = days(year);
		
		for (int i = 1; i < month; i++) 
		{
			if (isLeapYear(year)) 
			{
				totalDay += LEAP[i - 1];
			}
			else
			{
				totalDay += PLAIN[i - 1];
			}
		}
		return totalDay;
	}
	
	public int days(int year, int month, int day)
	{
		return days(year,month)+day;
	}
	
	public int lastDay(int year, int month)
	{
		return isLeapYear(year)?LEAP[month-1]:PLAIN[month-1];
	}
	
	public void calPrint(int year)
	{
		for (int i = 1; i <= 12; i++) 
		{
			calForm2(year, i);
		}
	}
	
	public void calForm(int year, int month)
	{
		int dayOfWeek = 1+(days(year,month,1)%7)-1;
		
		System.out.println();
		System.out.printf("		%d년\t%d월\n",year,month);
		System.out.println();
		System.out.println("일\t월\t화\t수\t목\t금\t토\t");
		
		for (int i = 0; i < dayOfWeek; i++) 
		{
			System.out.print("\t");
		}
		
		for (int i = 1; i <= lastDay(year, month); i++) 
		{
			System.out.print(i+"\t");
			if ((i+dayOfWeek)%7 == 0) 
			{
				System.out.println();
			}
		}
		System.out.println("");
		System.out.println();
	}
	
	public void calForm2(int year, int month)
	{
		int dayOfWeek = 1+(days(year,month,1)%7)-1;
		
		int dayOfMonth = checkMonth(year, month);
		
		System.out.println();
		System.out.printf("		%d년\t%d월\n",year,month);
		System.out.println();
		System.out.println("일\t월\t화\t수\t목\t금\t토\t");
		

		for (int i = dayOfMonth-dayOfWeek+1; i <= checkMonth(year, month); i++) 
		{
				System.out.print(i+"\t");
		}
		
		for (int i = 1; i <= lastDay(year, month); i++) 
		{
			System.out.print(i+"\t");
			
			if ((i+dayOfWeek)%7 == 0) 
			{
				System.out.println();
			}
		}
		
		for (int i = 1; i <= dayOfEnd(dayOfWeek,lastDay(year, month)); i++) 
		{
			System.out.print(i+"\t");
			
		}
		System.out.println();
	}
	
	public int dayOfEnd(int dayOfWeek, int lastDay)
	{
		int dayOfEnd = 7-((dayOfWeek+lastDay)%7);
		
		return dayOfEnd==7?0:dayOfEnd;
	}
	
	public int checkMonth(int year, int month)
	{
		int pastMonth = 0;
		
		if (month == 1) 
		{
			pastMonth = lastDay(year-1, month = 12);
		}
		else
		{
			pastMonth = lastDay(year, month-1);
		}
		
		return pastMonth;
	}
}
